package org.springframework.boot.tests.hibernate52.mytest.lifecycleprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.boot.tests.hibernate52.mytest.custombean.ManualRegisterBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.LifecycleProcessor;
import org.springframework.stereotype.Component;

@Component("lifecycleProcessor")
public class MyLifecycleProcessorTest implements LifecycleProcessor,ApplicationContextAware {

	private ApplicationContext applicationContext;

	@Override
	public void onRefresh() {
		ManualRegisterBean manualRegisterBean = (ManualRegisterBean) applicationContext.getBean(ManualRegisterBean.class.getName());
		manualRegisterBean.onLifecycleProcessorRefresh();
	}

	@Override
	public void onClose() {
		ManualRegisterBean manualRegisterBean = (ManualRegisterBean) applicationContext.getBean(ManualRegisterBean.class.getName());
		manualRegisterBean.onLifecycleProcessorClose();
	}

	@Override
	public void start() {

	}

	@Override
	public void stop() {

	}

	@Override
	public boolean isRunning() {
		return false;
	}

	/**
	 * org.springframework.context.support.ApplicationContextAwareProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
	 * 会在执行初始化方法的时候，通过Bean处理器进行注入
	 * @param applicationContext
	 * @throws BeansException
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
	}
}
